package cn.com.tt.flink.sql.engine.utils;

import java.util.function.Consumer;
import java.util.function.Function;

public class LambdaUtils {

    @FunctionalInterface
    public interface ThrowingConsumer<T, E extends Exception> {
        void accept(T t) throws E;
    }

    @FunctionalInterface
    public interface ThrowingFunction<T, R, E extends Exception> {
        R apply(T t) throws E;
    }

    public static <T> Consumer<T> consumerWrapper(ThrowingConsumer<T, Exception> thorwExceptionConsumer) {
        return i -> {
            try {
                thorwExceptionConsumer.accept(i);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
    }

    public static <T, R> Function<T, R> functionWrapper(ThrowingFunction<T, R, Exception> thorwExceptionFunction) {
        return i -> {
            try {
                return thorwExceptionFunction.apply(i);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
    }

}
